package com.stone.persistent.recyclerview.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.rishabhharit.roundedimageview.RoundedImageView;
import com.stone.persistent.recyclerview.R;
import com.stone.persistent.recyclerview.model.ProductItemModel;
import com.stone.persistent.recyclerview.utils.NetManager;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;


public class FeedsListAdapter extends RecyclerView.Adapter<FeedsListAdapter.GoodsViewHolder> {
    private Context context;
    private ArrayList dataList;

    @NotNull
    public GoodsViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_feeds_product, parent, false);
        return new GoodsViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(GoodsViewHolder holder, int position) {
       holder.bindPosition(position);
    }


    public int getItemCount() {
        return this.dataList.size();
    }




    public FeedsListAdapter(@NotNull FragmentActivity context) {
        this.context = context;
        this.dataList = NetManager.getProductList();
    }


    public final class GoodsViewHolder extends RecyclerView.ViewHolder {
        TextView textView,textViewPic;
        RoundedImageView roundedImageView;


        public void bindPosition(int position) {
            ProductItemModel itemData =(ProductItemModel)  FeedsListAdapter.this.dataList.get(position % 6);
            textView.setText(itemData.goodName);
            textViewPic.setText(itemData.price);

            roundedImageView.setImageResource(itemData.imagRes);
            ViewGroup.LayoutParams layoutParams = roundedImageView.getLayoutParams();

            ConstraintLayout.LayoutParams imageLp = (ConstraintLayout.LayoutParams) layoutParams;
            imageLp.dimensionRatio = String.valueOf(itemData.dimensionRatio);
            roundedImageView.setLayoutParams(imageLp);


        }

        public GoodsViewHolder(@NotNull View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.goods_title_tv);
            textViewPic = (TextView) itemView.findViewById(R.id.goods_price_tv);
            roundedImageView = ((RoundedImageView) itemView.findViewById(R.id.goods_imageview));
        }
    }
}
